$(function () {
    xsdtz_init();
})


function xsdtz_init() {
    //销售单位加载
//    $('#xsdtz_xsdw').combotree({
//        loadFilter: function (rows) {
//            return convert(rows);
//        },
//        onSelect: function (record) {
//            $('#xsdtz_xsr').combobox({
//                url: $.webPath() + '/sys/yg/ygXmList?zgjgId=' + record.id,
//                valueField: 'id',
//                textField: 'xm'
//            });
//        }
//    });

    $("[name='xsdtz_sbgs']").jgbmtree($("[name='xsdtz_sbbm']"), $("[name='xsdtz_sbr']"), '', '')


    //客户加载
    $("[name='xsdtz_kh']").qyjgzcombobox();
    //工程加载
    //$("[name='xsdtz_gc']").gccombobox();
    var options = getXsdtzGridOption();
    $('#xsdtz-grid').datagrid(options);

    //申请
    $("#xsdtz-apply").click(function () {
        var row = $('#xsdtz-grid').datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认', '是否确认申请冲销此销售单', function (r) {
                if (r) {
                    $.post($.webPath() + '/xsgl/xsdtz/xsdtzApply', {'xsdId': row.id}, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#xsdtz-grid').datagrid('reload');
                        } else {
                            $.messager.alert('提示', obj.message, 'info');
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', "请选择要冲销的销售单", 'info');
        }
    });
    //取消申请
    $("#xsdtz-cancel").click(function () {
        var row = $('#xsdtz-grid').datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认', '是否确认取消申请冲销此销售单', function (r) {
                if (r) {
                    $.post($.webPath() + '/xsgl/xsdtz/xsdtzCancel', {'xsdId': row.id}, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#xsdtz-grid').datagrid('reload');
                        } else {
                            $.messager.alert('提示', obj.message, 'info');
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', "请选择要取消冲销的销售单", 'info');
        }
    });
    //审批
    $("#xsdtz-examine").click(function () {
        var row = $('#xsdtz-grid').datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认', '是否确认审批冲销此销售单', function (r) {
                if (r) {
                    $.post($.webPath() + '/xsgl/xsdtz/xsdtzExamine', {'xsdId': row.id}, function (data) {
                        var obj = eval('(' + data + ')');
                        if (obj.statusCode == '200') {
                            $('#xsdtz-grid').datagrid('reload');
                        } else {
                            $.messager.alert('提示', obj.message, 'info');
                        }
                    });
                }
            });
        } else {
            $.messager.alert('提示', "请选择要审批的销售单", 'info');
        }
    });
}

function xsdtz_search() {
    var cxzt = $("#xsdtz-toolbar input[name='xsdtz_cxzt']").val();
    var xsdh = $("#xsdtz-toolbar input[name='xsdtz_xsdh']").val();
    var kh = $("#xsdtz-toolbar input[name='xsdtz_kh']").val();
//    var xsdw = $("#xsdtz-toolbar #xsdtz_xsdw").combotree('getValue');
//    var xsr = $("#xsdtz-toolbar input[name='xsdtz_xsr']").val();
    var sbgs = $("#xsdtz-toolbar input[name='xsdtz_sbgs']").val();
    var sbbm = $("#xsdtz-toolbar input[name='xsdtz_sbbm']").val();
    var sbr = $("#xsdtz-toolbar input[name='xsdtz_sbr']").val();
    var fhsjStart = $('#fhrqtz_start').datebox('getValue');
    var fhsjEnd = $('#fhrqtz_end').datebox('getValue');
    var options = $.extend(getXsdtzGridOption(),
        {queryParams: {cxzt: cxzt, xsdh: xsdh, khId: kh, sbgsId: sbgs, sbbmId: sbbm, sbrId: sbr, fhsjStart: fhsjStart, fhsjEnd: fhsjEnd}});
    $('#xsdtz-grid').datagrid(options);
}

function xsdtz_editItem(title) {
    var record = $("#xsdtz-grid").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要修改的记录!", "info");
        return;
    }
    var href = $.webPath() + "/xsgl/xsdtz/xsdtz_content?xsdId=" + record.id;
    xsdtz_showDialog(title, href);
}

//新增、修改企业法人弹出界面
var xsdtz_win;
function xsdtz_showDialog(title, href) {

    var button = [
        {text: '保存', handler: xsdtz_saveItem}
    ];

    xsdtz_win = $.window({
        title: title,
        iconCls: 'icon-add2',
        url: href,
        width: 1024,
        height: 505,
        buttons: button
    });
}

function xsdtz_saveItem() {
    xsdtz_win.mask();
    $("#xsdtz_mx").edatagrid('saveRow');
    $('#xsdtzForm').tabs('select', "销售信息");
    $("#xsdtzForm").form('submit', {
        url: $.webPath() + "/xsgl/xsdgl/edit_xsdtz",
        onSubmit: function (param) {
            var data = $('#xsdtz_mx').edatagrid("getData");
            var arrayPrefix = 'xsd.xsmxList';
            //物资计划明细
            var paramString = "{";
            //物资计划明细
            for (var i = 0; i < data.rows.length; i++) {
                {
                    var objectString = $.toJSONStringSubPrefix(data.rows[i], arrayPrefix + "[" + i + "].", ['jzfs.id', 'jzfs.z', 'hsj', 'bhsj', 'bz', 'sbsl', 'js', 'kh.id', 'jtwz.id', 'xscb', 'ck_sbsl']);
                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                }
            }
            if (data.rows && data.rows.length > 0)
                paramString = paramString.substring(0, paramString.length - 1);
            paramString += "}";
            $.extend(param, $.parseJSON(paramString));
            return $(this).form('validate');
        },
        success: function (data) {
            xsdtz_win.mask('hide');
            var obj = eval('(' + data + ')');
            if (obj.statusCode == '200') {
                $(xsdtz_win).window('close');
                $('#xsdtz-grid').datagrid('reload');
                $('#xsdtz_layout').layout('panel', 'center').panel('refresh');
                $.messager.show({title: '提示框', msg: obj.message, timeout: 3000, showType: 'slide'});
            } else {
                $.messager.alert('提示', obj.message, 'info');
            }
        }
    });
}


function getXsdtzGridOption() {
    var options = {
        url: $.webPath() + "/xsgl/xsdgl/xsdList",
        toolbar: "#xsdtz-toolbar",
        fitColumns: true,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        onLoadSuccess: function (target) {
            $("#xsdtz-edit").css('display', 'none');
            $(this).datagrid('selectRow', 0);

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        loadFilter: function (data) {
            $.each(data.rows, function () {
                this.tzsqzt = this.tzsqzt == 'wsq' ? "未申请" : this.tzsqzt == 'ysq' ? "已申请" : this.tzsqzt == 'ysp' ? "已审批" : "未申请";
            })
            return data;
        },
        onSelect: function (rowIndex, rowData) {
            if (rowData.xslx != 'zddh') {
                if (rowData && (rowData.tzsqzt == '未申请' || rowData.tzsqzt == '' )) {
                    $("#xsdtz-apply").show();
                    $("#xsdtz-cancel").hide();
                    $("#xsdtz-examine").hide();
                    $("#xsdtz-edit").hide();
                } else if (rowData && rowData.tzsqzt == '已申请') {
                    $("#xsdtz-apply").hide();
                    $("#xsdtz-cancel").show();
                    $("#xsdtz-examine").show();
                    $("#xsdtz-edit").hide();
                } else {
                    $("#xsdtz-apply").hide();
                    $("#xsdtz-cancel").hide();
                    $("#xsdtz-examine").hide();
                    if (rowData && !rowData.sftzd && rowData.xslx != 'zddh')
                        $("#xsdtz-edit").css('display', '');
                    else
                        $("#xsdtz-edit").css('display', 'none');
                }
            }else{
                $("#xsdtz-apply").hide();
                $("#xsdtz-cancel").hide();
                $("#xsdtz-examine").hide();
                $("#xsdtz-edit").hide();
            }
        }, onDblClickCell: function (index, field, value) {
            if (field == "xsdh") {
                $("#xsdtz-edit:visible").click();
                //xsdtz_editItem('调整销售单');
            }
        },
        rowStyler: function (index, row) {
            if (row.sftzd) {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    }

    var columns = {columns: [
        [
            {field: 'xsdh', title: '销售单号', align: 'center', width: 110, formatter: xsdtzCell},
            {field: 'fhsj', title: '发货日期', align: 'center', width: 67},
            {field: 'kh_qc', title: '客户名称', align: 'center', width: 147},
            {field: 'gc_mc', title: '工程名称', align: 'center', width: 50},
            {field: 'xssl', title: '销售数量', align: 'right',
                formatter: function (value) {
                    var sl = formatSlForHtml(value, ",");
                    return sl;
                }, width: 97},
            {field: 'xsje', title: '销售金额(元)', align: 'right', width: 97,
                formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }
            },
            {field: 'thck_mc', title: '提货仓库', align: 'center', width: 58},
            {field: 'fyzr_z', title: '配送类型', align: 'center', width: 59},
            {field: 'dsyfje', title: '配送费用', align: 'right', width: 91,
                formatter: function (value) {
                    var sl = formatJeForHtml2(value, ",");
                    return sl;
                }
            },
            {field: 'xsgs_jc', title: '销售公司', align: 'center', width: 78},
            {field: 'sbgs_jc', title: '申报公司', align: 'center', width: 65},
            {field: 'sbbm_jc', title: '申报部门', align: 'center', width: 64},
            {field: 'sbr_xm', title: '申报人', align: 'center', width: 44},
            {field: 'tzsqzt', title: '调整申请状态', align: 'center', width: 52}
        ]
    ]}
    options = $.extend(options, columns);
    return options;
}